<template>
	<view class="contain">
			<block v-if="!hasLogin || userData.userinfo===null">
				<view class="w-[88%] m-auto flex justify-center mt-[100rpx] fonttext blod yuyuebtn lookbg" @click="goLogin">
					<u-button type="primary" shape="circle">登录后获取报告</u-button>
				</view>				
			</block>
			<block v-else>
				<view class="w-[80%] m-auto flex justify-between items-center mt-2" >
					<view class="text-lg font-bold" v-if="username!=null">{{username}}</view>
					<view class="text-lg font-bold" v-else>{{ hiddenPhone(store.phone)}}</view>
					<view class="fonttext" v-if="useridcard!=null" @click="changeidcard">{{ hideIdCard(useridcard)}}</view>
					<view v-else class="fonttext">未绑定身份证</view>
				</view>	
				<!-- {{courseLink!=null}}
				{{courseLink}} -->
				<view v-if="courseLink!=null" class="w-[88%] m-auto flex justify-center mt-2 fonttext blod yuyuebtn lookbg" @click="openCourseLink">
					<u-button type="primary" shape="circle">{{btnText}}</u-button>
				</view>	
				<view v-else class="w-[88%] m-auto flex justify-center mt-2 fonttext blod yuyuebtn lookbg" @click="openReportLink">
					<u-button type="primary" shape="circle">查看测评报告</u-button>
				</view>	
			</block>
			<view class="w-full flex justify-center mt-3"><text class="underline text-sm" @click="goHome">返回首页</text></view>
			<!-- 弹出层 -->
			<view class="popup">
			<u-popup :show="showInput" :customStyle="popuCustyle" @close="close" @open="open" mode="center">
					<view class="head">
						<text>请填写身份证后进行测评</text>
					</view>
					<view class="body">
						<view> 
							<u--input
						    placeholder="请输入身份证号码"
						    border="surround"
							maxlength="18"
						    v-model="idcardValue"></u--input>
						</view>
						<view class="mt-5 mb-2">
							<u-button type="primary" :customStyle="{'width':'300rpx'}" text="确定" @click="submitData"></u-button>
						</view>
					</view>
			</u-popup>
			</view>
	</view>
</template>

<script setup lang='ts'>
import { ref,reactive } from 'vue'
import {  onLoad,onShow } from '@dcloudio/uni-app'
import { userStore } from '@/stores/userStore'
import {hideIdCard,hiddenPhone} from '@/common/common.js'


const store = userStore();
let { hasLogin,userData } = userStore();

let username=ref(null);
let useridcard=ref(null);

let courseLink=ref(null);
let reportLink=ref(null);

let dataList=reactive({data:[]});
let errormsg=ref('');
let download=ref('');

let isIdentity=ref(false);
let showInput=ref(false);
let idcardValue=ref(null);

let btnText=ref('点击开始测评');

let popuCustyle=reactive({
	"min-width":"600rpx",
	"min-height":"300rpx",
	"border-radius":"20rpx",
	"box-sizing":"border-box",
	"padding":"0rpx"
});


const goHome=()=>{
	uni.$tools.GoUrl("/pages/index/index",2);
}


//提交身份证
const submitData=()=>{
	if(idcardValue.value===null){
		uni.$tools.Toast("请输入身份证号码");
		return;
	}
	if(userData.userinfo.id===null){
		uni.$tools.Toast("账号异常 请重新登陆");
		return;
	}
	let paraData={
		id:userData.userinfo.id,
		idcard:idcardValue.value
	}
	console.log("提交身份证请求参数:",paraData);
	uni.$tools.Reqeust('/api/addIdCard',paraData,'POST').then((res:any)=>{
		console.log("提交身份证结果:",res);
		if(res.data.code===200){
			showInput.value=false;
			isIdentity.value=true;
			userData.userinfo.idcard=idcardValue.value;
			useridcard.value=idcardValue.value;
			uni.$tools.Toast("身份证已更新");
		}
	});
}


const changeidcard=()=>{
	idcardValue.value=userData.userinfo.idcard;
	showInput.value=true;
}

const close=()=>{
	showInput.value=false;
}


const open=()=>{
	showInput.value=true;
}

//内部第三方登录
const innerLoign=()=>{
	if(userData.userinfo===null || hasLogin===false){
		//uni.$tools.Toast("尚未登录");
		console.log("尚未登录");
		return;
	}
	let paraData={
		indexNumber:1,
		userNumber:userData.userinfo.id, //userData.userinfo.id  // user11475 福建数据库对应的id
		phone:store.phone,
		idcard:userData.userinfo.idcard
	}
	console.log("请求参数:",paraData);
	uni.$tools.Reqeust('/webservices/mbLogin',paraData,'POST').then((res:any)=>{
		console.log("内部第三方登录结果:",res);
		if(res.data.code===200){
			if(res.data.data!=null && res.data.data.indexOf("https")!=-1){
				courseLink.value=res.data.data;
				console.log("登录成功，获取测试链接...",courseLink.value);				
			}else{
				//获取量表结果数据
				getResultData();
			}
		}else{
			if(res.data.errMsg.indexOf("测试记录未完成")!=-1){
				getCourseH5Link();
			}
		}
	});
}




//获取量表结果数据
const getResultData=()=>{
	let paraData={
		sessionIdList:userData.userinfo.sessionid //测试: 1855181271290875904 
	}
	uni.$tools.Reqeust('/webservices/getResult',paraData,'POST').then((res:any)=>{
		console.log("获取量表结果数据:",res);
		if(res.data.code===200){
			if(res.data.data!=null){
				let json=JSON.parse(res.data.data);
				if(json.data.recordList[0].reportLink==='' || json.data.recordList[0].status==='未开始'){
					uni.$tools.Toast("课程还未开始");
					getCourseH5Link();
				}else{
					//获取报告
					reportLink.value=json.data.recordList[0].reportLink;
				}
			}
		}
	});
}

//获取课程地址进行测试
const getCourseH5Link=()=>{
	let paraData={
		userNumber:userData.userinfo.id, //userData.userinfo.id // user11475 福建数据库对应的id
		sessionid:userData.userinfo.sessionid //测试: 1855181271290875904 
	}
	uni.$tools.Reqeust('/webservices/getH5Link',paraData,'POST').then((res:any)=>{
		console.log("获取课程地址进行测试数据:",res);
		if(res.data.code===200){
			btnText.value="您有测试尚未完成，点击继续测评";
			courseLink.value=res.data.data;
		}
	});
}


//登录
const goLogin=()=>{
	let keyid=uni.getStorageSync("keyid") || null;
	let url="/pages/login/login";
	if(keyid!=null){
		url="/pages/login/login?keyid="+keyid;
	}
	console.log("跳转登录链接",url);
	uni.$tools.GoUrl(url,1);
}


//打开学习链接
const openCourseLink=()=>{
	
	if(!isIdentity.value){
		showInput.value=true;
		return;
	}
	
	if(courseLink.value!=null){
		store.backRouter=courseLink.value;
		uni.$tools.GoUrl("/pages/webview/webview");
	}
}


//打开链接报告
const openReportLink=()=>{
	if(reportLink.value!=null){
		store.backRouter=reportLink.value;
		uni.$tools.GoUrl("/pages/webview/webview");
	}
}



onLoad((e:any)=>{
	if(e!=null){
		if(e.keyid!=null && e.keyid!=''){
			uni.setStorageSync("keyid",e.keyid);
		}		
	}
	innerLoign();
});

onShow(()=>{
	// if(userData.userinfo===null || hasLogin===false){
	// 	uni.$tools.Toast("会话失效请重新登录");
	// 	setTimeout(()=>{
	// 		uni.$tools.GoUrl('/pages/login/login',0);
	// 	},1000);
	// 	return;
	// }
	if(userData.userinfo!=null){
		username.value=userData.userinfo.username;
		if(userData.userinfo.idcard!=null && userData.userinfo.idcard!=''){
			useridcard.value=userData.userinfo.idcard;
			isIdentity.value=true;
		}
		
	}
})
</script>

<style lang='scss' scoped>
	@import "getcepingresullt.scss";
</style>